#include <string.h>
#include <iostream>
using namespace std;

class Computer{
public:
    Computer(const char *name, float price)
    :_name(new char[strlen(name) + 1]())
    ,_price(price)
    {
        strcpy(_name, name);
        _totalprice += _price;
        cout << "Computer(char *, float )" << endl;
    }

    //创建静态函数来返回静态数据的值
    static float getInstance(){
        return _totalprice;
    }

    //拷贝构造函数
    Computer(const Computer & rhs)
    :_name(new char[strlen(rhs._name) + 1]())
    ,_price(rhs._price)
    {
        strcpy(_name, rhs._name);
        _totalprice += _price;
    }

    //赋值运算函数
    Computer & operator=(const Computer & rhs){
        //考虑字赋值的情况
        if(this != &rhs){
            //先清空目标函数的原来的内存空间
            delete [] _name;
            //申请新的堆空间进行深拷贝
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name, rhs._name);

            //进行赋值运算之后的价格
            //先要把之前这个目标对象的价格删去
            _totalprice -= _price;
            //此时目标对象的价格换成复制对象的价格
            _price = rhs._price;
            //加上新的价格
            _totalprice += _price;
        }
        return *this;
    }

    //修改价格，在跟换新的价格
    void setPrice(float price){
        _totalprice -= _price;
        _price = price;
        _totalprice += _price;
    }

    ~Computer(){
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }

private:
    char * _name;
    float _price;
    static float _totalprice;
};
float Computer::_totalprice = 0;

void test(){

    cout << Computer::getInstance() << endl;
    Computer c1("Apple Macbook air",12000);
    cout << Computer::getInstance() << endl;
    Computer c2("Xiaomi Macbook",5999);
    cout << Computer::getInstance() << endl;
    Computer c3("HUAWEI Macbook", 9999);
    cout << Computer::getInstance() << endl;
    Computer c4("Redmi Macbook",4999);
    cout << Computer::getInstance() << endl;
    c4 = c3;
    Computer c5 = c1;
    cout << Computer::getInstance() << endl;

}

int main(int atgc, char * argv[])
{
    test();
    return 0;
}

